<?php

namespace App\Http\Controllers\Home\User;

use App\Http\Controllers\Controller;
use App\Models\Home\Deliver as DeliverModel;
use App\Models\Home\Finance;
use Frame;
use Illuminate\Support\Facades\Auth;
use Tools;
use FileSystem;

class DeliverScheduleController extends Controller
{
    protected $passField = [
        'deliver_id','plan_time','try_wage','wage','content','uid','result'
    ];
    protected $resume;
    protected $resume_pend;

    public function __construct()
    {
        $this->resume = get_config_cache('resume');
        $this->resume_pend = get_config_cache('resume_pend');
    }


    public function add_resume_schedule(DeliverModel $obj){
        $obj->load('resume');
        if(request() ->isMethod('post')){
            //验证
            $rules = [
                'plan_time' => 'required|string',
                'try_wage' => 'required|string',
                'wage' => 'required|string',
                'content' => 'nullable|string',
                'result' => [function ($attribute, $value, $fail) {
                    if (!$value) {
                        $fail(':attribute 未选择。');
                    }
                }],
                'shareTo' => 'required|integer'
            ];

            $customAttributes = [
                'plan_time'   => '预计入职时间',
                'try_wage'   => '试用期薪资',
                'wage'   => '转正薪资',
                'content'   => '沟通记录',
                'result'   => '跟进结果',
                'shareTo' => '任务归属',
            ];

            if (intval(request()->post('result')) < 10){
                unset($rules['plan_time'],$rules['try_wage'],$rules['wage']);
            }

            if(!in_array(intval(request()->post('result')),[5,7,11])){
                unset($rules['shareTo']);
            }

            $this->validate(request(), $rules,[],$customAttributes);

            //数据库操作
            $data = [];
            foreach (request()->all() as $key => $val){
                if (!is_numeric($val)){	$val = clean($val);}
                if(in_array($key,$this->passField)){
                    $data[$key] = $val;
                }
            }
            $data['uid'] = Auth::id();

            if (in_array(intval($data['result']),[5,7,11])){
                switch (intval(request()->post('shareTo'))){
                    case 1:
                        $obj->resume->assign = Auth::id();
                        $obj->resume->assign_time = strtotime('+ '.$this->resume.' day',time());
                        $obj->resume->share = 0;
                        break;
                    case 2:
                        $obj->resume->assign = '';
                        $obj->resume->assign_time = '';
                        $obj->resume->share = 1;
                        break;
                    case 3:
                        if ($assign = intval(request()->post('assign'))){
                            $obj->resume->assign = $assign;
                            $obj->resume->assign_time = strtotime('+ '.$this->resume.' day',time());
                            $obj->resume->share = 0;
                        }else{
                            $obj->resume->assign = '';
                            $obj->resume->assign_time = '';
                            $obj->resume->share = 1;
                        }
                        break;
                    default:
                        $obj->resume->assign = '';
                        $obj->resume->assign_time = '';
                        $obj->resume->share = 1;
                }
                $obj->resume->save();
            }

            if ($data['result'] >= 12){
                if ($data['result'] == 12){
                    $obj->resume->assign_time = strtotime('+ '.$this->resume_pend.' day',time());
                    $obj->resume->save();
                }
                if ($data['result'] == 13){
                    $obj->load('position');
                    Finance::updateOrCreate(['resume_id'=>$obj->resume->id,'position_id'=>$obj->position->id],['client_id'=>$obj->position->client_id,'assign'=>$obj->resume->assign,'entry_time'=>date('Y-m-d H:i:s',time())]);
                }
            }
            $data['assign'] = $obj->resume->assign;
            $obj->all_schedule()->updateOrCreate(['result'=>$data['result']],$data);


            //返回结果
            return ['code'=>1];
        }

        $frame = Frame::GetFrame(config('base.branch.lg.id'),config('base.level.admin'));

        $obj->load(['all_schedule.user']);

        $obj->load(['one_schedule'=>function($query){
            $query->orderBy('id','desc');
        }]);

        $obj->resume->load(['user','assign_user.frame']);

        if (count($obj->all_schedule)){

            foreach ($obj->all_schedule as $k => $v){

                $obj->all_schedule[$k]->user_name = $v->user?$v->user->username:'';

                $obj->all_schedule[$k]->team_name = $v->user?$v->user->frame?$v->user->frame->belong_user?$v->user->frame->belong_user->username:'':'':'';

                $result = $v->result;

                switch ($result){
                    case 1:
                        $schedule_result = '待推荐';
                        break;
                    case 2:
                        $schedule_result = '已推荐';
                        break;
                    case 3:
                        $schedule_result = '筛选通过';
                        break;
                    case 4:
                        $schedule_result = '安排一面';
                        break;
                    case 5:
                        $schedule_result = '一面fail';
                        break;
                    case 6:
                        $schedule_result = '安排二面';
                        break;
                    case 7:
                        $schedule_result = '二面fail';
                        break;
                    case 8:
                        $schedule_result = '面试通过';
                        break;
                    case 9:
                        $schedule_result = 'offer中';
                        break;
                    case 10:
                        $schedule_result = '接offer';
                        break;
                    case 11:
                        $schedule_result = '拒offer';
                        break;
                    case 12:
                        $schedule_result = '待入职';
                        break;
                    case 13:
                        $schedule_result = '已入职';
                        break;
                    default:
                        $schedule_result = '待推荐';
                }

                $obj->all_schedule[$k]->schedule_result = $schedule_result;
            }
        }

        $obj->resume = Tools::check_permission($obj->resume);

        if ($obj->resume->assign){
            if ($obj->resume->assign == Auth::id()){
                $obj->resume->shareTo = 1;
            }else{
                $obj->resume->shareTo = 3;
            }
        }else{
            $obj->resume->shareTo = 2;
        }

        $resume_day = $this->resume;

        return view('Home.User.Deliver.add_resume_schedule',compact('obj','frame','resume_day'));
    }

    public function add_position_schedule(DeliverModel $obj){
        $obj->load('position');
        if(request() ->isMethod('post')){
            //验证
            $rules = [
                'plan_time' => 'required|string',
                'try_wage' => 'required|string',
                'wage' => 'required|string',
                'content' => 'nullable|string',
                'result' => [function ($attribute, $value, $fail) {
                    if (!$value) {
                        $fail(':attribute 未选择。');
                    }
                }],
                'shareTo' => 'required|integer'
            ];

            $customAttributes = [
                'plan_time'   => '预计入职时间',
                'try_wage'   => '试用期薪资',
                'wage'   => '转正薪资',
                'content'   => '沟通记录',
                'result'   => '跟进结果',
                'shareTo' => '任务归属',
            ];

            if (intval(request()->post('result')) < 10){
                unset($rules['plan_time'],$rules['try_wage'],$rules['wage']);
            }

            $this->validate(request(), $rules,[],$customAttributes);

            //数据库操作
            $data = [];
            foreach (request()->all() as $key => $val){
                if (!is_numeric($val)){	$val = clean($val);}
                if(in_array($key,$this->passField)){
                    $data[$key] = $val;
                }
            }
            $data['uid'] = Auth::id();

            switch (intval(request()->post('shareTo'))){
                case 1:
                    $obj->position->assign = Auth::id();
                    $obj->position->share = 0;
                    break;
                case 2:
                    $obj->position->assign = '';
                    $obj->position->share = 1;
                    break;
                case 3:
                    if ($assign = intval(request()->post('assign'))){
                        $obj->position->assign = $assign;
                        $obj->position->share = 0;
                    }else{
                        $obj->position->assign = '';
                        $obj->position->share = 1;
                    }
                    break;
                default:
                    $obj->position->assign = '';
                    $obj->position->share = 1;
            }

            $obj->position->save();

            if ($data['result'] >= 12){
                if ($data['result'] == 12){
                    $obj->resume->assign_time = strtotime('+ '.$this->resume_pend.' day',time());
                    $obj->resume->save();
                }
                if ($data['result'] == 13){
                    $obj->load('resume');
                    Finance::updateOrCreate(['resume_id'=>$obj->resume->id,'position_id'=>$obj->position->id],['client_id'=>$obj->position->client_id,'assign'=>$obj->resume->assign,'entry_time'=>date('Y-m-d H:i:s',time())]);
                }
            }
            $data['assign'] = $obj->resume->assign;
            $obj->all_schedule()->updateOrCreate(['result'=>$data['result']],$data);


            //返回结果
            return ['code'=>1];
        }

        $frame = Frame::GetFrame(config('base.branch.lg.id'),config('base.level.admin'));

        $obj->load(['all_schedule.user']);

        $obj->load(['one_schedule'=>function($query){
            $query->orderBy('id','desc');
        }]);

        $obj->position->load(['user','assign_user.frame']);

        if (count($obj->all_schedule)){

            foreach ($obj->all_schedule as $k => $v){

                $obj->all_schedule[$k]->user_name = $v->user?$v->user->username:'';

                $obj->all_schedule[$k]->team_name = $v->user?$v->user->frame?$v->user->frame->belong_user?$v->user->frame->belong_user->username:'':'':'';

                $result = $v->result;

                switch ($result){
                    case 1:
                        $schedule_result = '待推荐';
                        break;
                    case 2:
                        $schedule_result = '已推荐';
                        break;
                    case 3:
                        $schedule_result = '筛选通过';
                        break;
                    case 4:
                        $schedule_result = '安排一面';
                        break;
                    case 5:
                        $schedule_result = '一面fail';
                        break;
                    case 6:
                        $schedule_result = '安排二面';
                        break;
                    case 7:
                        $schedule_result = '二面fail';
                        break;
                    case 8:
                        $schedule_result = '面试通过';
                        break;
                    case 9:
                        $schedule_result = 'offer中';
                        break;
                    case 10:
                        $schedule_result = '接offer';
                        break;
                    case 11:
                        $schedule_result = '拒offer';
                        break;
                    case 12:
                        $schedule_result = '待入职';
                        break;
                    case 13:
                        $schedule_result = '已入职';
                        break;
                    default:
                        $schedule_result = '待推荐';
                }

                $obj->all_schedule[$k]->schedule_result = $schedule_result;

            }

        }

        $obj->position = Tools::check_permission($obj->position);

        if ($obj->position->assign){
            if ($obj->position->assign == Auth::id()){
                $obj->position->shareTo = 1;
            }else{
                $obj->position->shareTo = 3;
            }
        }else{
            $obj->position->shareTo = 2;
        }

        return view('Home.User.Deliver.add_position_schedule',compact('obj','frame'));
    }

}